
using System.Net.NetworkInformation;

namespace WinFormsApp1
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();

            Form1.Port = GetPort();

            Task.Run(() => DbJobsApi.Startup.Run(Form1.Port));

            Application.Run(new Form1());
        }

        public static int GetPort()
        {
            var arr = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
            return (from a in Enumerable.Range(15888, 100)
                    join b in arr on a equals b.Port into bb
                    from bbb in bb.DefaultIfEmpty()
                    where bbb == null
                    select a).FirstOrDefault();
        }
    }
}